home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7003 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: news.iadfw.net!usenet
  2. From: Mark Nelson <markn@airmail.net>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: STL
  5. Date: Wed, 21 Feb 1996 08:36:31 -0600
  6. Organization: customer of Internet America
  7. Message-ID: <312B2DEF.65C7@airmail.net>
  8. References: <4gdgt0$494@netaxs.com>
  9. NNTP-Posting-Host: dal14-23.ppp.iadfw.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
  14.  
  15. Bob Pesavento wrote:
  16. > I'm a newbie with STL and not an expert at C++ so bear with me.  Assume a
  17. > simplified class:
  18. > class myclass
  19. > {
  20. >   int myint;
  21. >   float myfloat;
  22. >   public:...
  23. > I want use "list" and stuff a bunch of these in using push_back.  No
  24. > problem so far.  But I want the user to be able to select which item to
  25. > sort on... myint or myfloat.  
  26.  
  27. Hi Bob,
  28.  
  29. The sorting is done using operator<(), so you don't have much choice about that. 
  30. In your case, I would create a static class member that defined which sort
  31. of sorting you wanted (excuse any syntax errors, this is on they fly):
  32.  
  33. class myclass {
  34.   public :
  35.     static enum {
  36.         SORT_ON_INT,
  37.         SORT_ON_FLOAT } kind;
  38.  
  39. Then, in operator<(), you do this:
  40.  
  41.     if ( kind == SORT_ON_INT )
  42.         return myint < rhs.myint;
  43.     else
  44.         return myfloat < rhs.myfloat.
  45.  
  46. The catch behind all this is that you had better set myclass::kind
  47. to the proper value before you insert anything into your map or
  48. set.  For the list<>, before you sort.
  49.  
  50. Mark Nelson
  51. http://web2.airmail.net/markn  -- more STL pointers
  52.